package cn.qiyu5522.tk_ems.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.qiyu5522.tk_ems.entity.JobCategory;
import cn.qiyu5522.tk_ems.entity.dto.JobCategoryDTO;
import cn.qiyu5522.tk_ems.entity.dto.QueryParamDTO;
import cn.qiyu5522.tk_ems.entity.vo.JobCategoryVO;
import cn.qiyu5522.tk_ems.mapper.JobcategoryMapper;
import cn.qiyu5522.tk_ems.result.CustomMapResult;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.service.JobCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Tonkey
 * @since 2023-02-22
 */
@Service
public class JobCategoryServiceImpl extends ServiceImpl<JobcategoryMapper, JobCategory> implements JobCategoryService {
    @Resource
    private ObjectMapper objectMapper;

    @Override
    public R queryJobCategoryByPageCondition(QueryParamDTO<JobCategoryDTO> queryParamDTO) {
         // 查询条件 --- 分类名称<->编号
        LambdaQueryWrapper<JobCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 分页
        IPage<JobCategory> jobCategoryIPage = new Page<>(queryParamDTO.getCurrentPage(),queryParamDTO.getPageSize());
        JobCategoryDTO condition = queryParamDTO.getCondition();
        // 实体类转Vo对象
        List<JobCategoryVO> jobCategoryVOS = new ArrayList<>();
        // 封装返回结果
        Map<String, Object> map = new HashMap<>();
        if(ObjectUtil.isNotEmpty(condition)){
            Integer pid = condition.getPid();
            String name = condition.getName();
                    // 父id
            lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(pid),JobCategory::getPid,pid)
                    // 工作名称
            .like(ObjectUtil.isNotEmpty(name),JobCategory::getName,name)
                    // 过滤父级节点
            .isNotNull(JobCategory::getPid)
            .orderByDesc(JobCategory::getId);
            baseMapper.selectPage(jobCategoryIPage,lambdaQueryWrapper);
            jobCategoryVOS = objectMapper.convertValue(jobCategoryIPage.getRecords(), new TypeReference<List<JobCategoryVO>>() {
            });
            // 查询所有父级
            List<JobCategory> jobCategories = baseMapper
                    .selectList(new LambdaQueryWrapper<JobCategory>().isNull(JobCategory::getPid));
            for (JobCategory jobCategory : jobCategories) {
                jobCategoryVOS.forEach(jobCategoryVO -> {
                    if(jobCategoryVO.getPid().equals(jobCategory.getId())){
                        jobCategoryVO.setPname(jobCategory.getName());
                    }
                });
            }
            // 封装返回结果
            map = CustomMapResult
                    .packagingResult(jobCategoryVOS, queryParamDTO.getCurrentPage(), queryParamDTO.getPageSize(), jobCategoryIPage.getTotal());
            return R.ok().data("jobCategoryIPage",map);
        }
        // 查询所有子数据
        lambdaQueryWrapper.clear();
        lambdaQueryWrapper.isNotNull(JobCategory::getPid)
        .orderByDesc(JobCategory::getId);
        baseMapper.selectPage(jobCategoryIPage,lambdaQueryWrapper);
        // 实体类转Vo对象
        jobCategoryVOS = objectMapper.convertValue(jobCategoryIPage.getRecords(), new TypeReference<List<JobCategoryVO>>() {
        });
        map = CustomMapResult
                .packagingResult(jobCategoryVOS, queryParamDTO.getCurrentPage(), queryParamDTO.getPageSize(), jobCategoryIPage.getTotal());
        return R.ok().data("jobCategoryIPage",map);
    }

    @Override
    public R queryForCategoryList() {
        LambdaQueryWrapper<JobCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.isNull(JobCategory::getPid);
        List<JobCategory> jobCategories = baseMapper.selectList(lambdaQueryWrapper);
        // 实体类转Vo对象
        List<JobCategoryVO> jobCategoryVOS = objectMapper.convertValue(jobCategories, new TypeReference<List<JobCategoryVO>>() {
        });
        return R.ok().data("categories",jobCategoryVOS);
    }

    @Override
    public R saveMore(List<JobCategoryDTO> jobCategoryDTOs) {
       // DTO 转实体类
        // 实体类转Vo对象
        List<JobCategory> jobCategories = objectMapper.convertValue(jobCategoryDTOs, new TypeReference<List<JobCategory>>() {
        });
        // 查出当前类别下子项的最大id
        for (JobCategory jobCategory : jobCategories) {
            int id =  baseMapper.generateId(jobCategory.getPid());
            jobCategory.setId(id);
            jobCategory.setHasChild(0);
        }
        Integer integer = baseMapper.insertBatchSomeColumn(jobCategories);
        return integer>0? R.ok().message("添加成功"):R.error().message("添加失败");
    }

    @Override
    public R queryByName(String name) {
        LambdaQueryWrapper<JobCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JobCategory::getName,name);
        List<JobCategory> jobCategories = baseMapper.selectList(lambdaQueryWrapper);
        return jobCategories.size()==0?
                R.ok().message("名称可用").data("flag",true):
                R.ok().message("名称不可用").data("flag",false);
    }

    @Override
    public R queryJobNameAndIdListByPid(Long pid) {
        LambdaQueryWrapper<JobCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JobCategory::getPid,pid)
        .select(JobCategory::getId,JobCategory::getName);
        List<JobCategory> jobNameAndIdList = baseMapper.selectList(lambdaQueryWrapper);
        return R.ok().data("jobNameAndIdList",jobNameAndIdList);
    }
}
